package com.wiseman.writing.utility;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Point;

public class BSJavaApi{

	public BSJavaApi(Context context, int runMode, int codePage) {
		m_bsjni = BSJniApi.createBSJniApi(context, runMode, codePage);
		m_strokeNum = 0;
	}

	public BSJniApi getBSJniApi() {
		return m_bsjni;
	}

	// 为某锟斤拷锟斤拷准锟斤拷
	public boolean prepareData(char code) {

		if (m_bsjni.prepareData(code) != 0) {
			m_strokeNum = m_bsjni.getStrokeNum();
			return true;
		}

		return false;
	}
	
	// 锟矫碉拷锟绞伙拷锟斤�?
	public int getStrokeNum() {
		return m_strokeNum;
	}

	// 锟斤拷锟斤拷锟斤拷一锟斤�?
	public List<StrokeData> getCharData(int fontsize) {
		
		List<StrokeData> dataList = new ArrayList<StrokeData>();
		StrokeData data = null;
		
		for(int index = 0; index < m_strokeNum; index ++){
			int[] resPoint = m_bsjni.getDrawPoint(fontsize);
			int[] flagTable = m_bsjni.getFlagTab();
			
			data = getStrokeData(resPoint, flagTable);
			dataList.add(data);
		}
	
		return dataList;
	}

	// 锟矫碉拷锟斤拷锟酵拷锟斤拷锟斤拷牡锟斤拷锟斤拷锟�
	private StrokeData getStrokeData(int[] in, int[] flagTable) {
		int pointArrayLen = in.length;
		int posIndex, negIndex;
		int syncPosA = 0, syncPosB = 0;

		StrokeData data = new StrokeData();
		
		ArrayList<Point> posList = new  ArrayList<Point>();
		ArrayList<Point> negList = new  ArrayList<Point>();
		posIndex = 0;
		negIndex = pointArrayLen - 1;

		posList.add(new Point(in[0], in[1]));
		negList.add(new Point(in[0], in[1]));
	
		posIndex += 2;
		negIndex -= 2;

		int syncPointSize = 0;

		if (flagTable[0] != 0) {
			syncPointSize = flagTable.length - 1;
			syncPosA = 0;
			syncPosB = syncPointSize - 1;
		}

		while (posIndex < negIndex) {
			if (syncPointSize > 0 && posIndex == flagTable[syncPosA] && syncPosA < syncPointSize) {// 锟斤拷锟斤拷锟斤拷同锟斤拷锟斤拷
				syncPosA++;
				while (negIndex != flagTable[syncPosB]) {
			
					posList.add(new Point(in[posIndex], in[posIndex+1]));
					negList.add(new Point(in[negIndex], in[negIndex+1]));
		
					negIndex -= 2;
				}
				syncPosB--;
			} else if (syncPointSize > 0 && negIndex == flagTable[syncPosB] && syncPosB > 0) {// 锟斤拷锟斤拷锟斤拷同锟斤拷锟斤拷
				syncPosB--;
				while (posIndex != flagTable[syncPosA]) {
				
					posList.add(new Point(in[posIndex], in[posIndex+1]));
					negList.add(new Point(in[negIndex], in[negIndex+1]));
					posIndex += 2;
				}
				syncPosA++;
			} else {
			
				posList.add(new Point(in[posIndex], in[posIndex+1]));
				negList.add(new Point(in[negIndex], in[negIndex+1]));
				posIndex += 2;
				negIndex -= 2;
			}
		}
		
		data.m_posList = posList;
		data.m_negList = negList;
		
		return data;
	}


	public void unLoad() {
		m_bsjni.unLoad();
	}

	private BSJniApi m_bsjni;
	private int m_strokeNum;
}
